package com.luoye.lj.wstool.wsdm;

import com.luoye.lj.util.StringUtil;
import com.luoye.lj.wstool.common.StringUtils;
import com.luoye.lj.wstool.generator.ICodeGenerator;
import com.luoye.lj.wstool.wsdm.entity.WsdtEntity;
import com.luoye.lj.wstool.wsdm.entity.WsdtField;

/*
 * Created with IntelliJ IDEA.
 * User: Dave Liu
 * Date: 2016/4/26
 * Time: 15:43
 * To change this template use File | Settings | File Templates.
 */
public class FieldEntityServicesInjectionGenerator implements ICodeGenerator {
    private WsdtEntity wsdtEntity;

    public FieldEntityServicesInjectionGenerator(WsdtEntity wsdtEntity) {
        this.wsdtEntity = wsdtEntity;
    }

    @Override
    public String codeGenerate() {
        String code="";
        if(this.wsdtEntity!=null){
            if(this.wsdtEntity.getFieldList()!=null){
                for(WsdtField wsdtField:this.wsdtEntity.getFieldList()){
                    if(wsdtField.getWsdtFieldDesc()!=null&&wsdtField.getWsdtFieldDesc().getRelatedEntity()!=null){
                        if(code.indexOf("I" + wsdtField.getWsdtFieldDesc().getRelatedEntity().getEntityName() + "Service")==-1) {
                            code += "    @Autowired\n" +
                                    "    private I" + wsdtField.getWsdtFieldDesc().getRelatedEntity().getEntityName() + "Service " +
                                    StringUtils.makeFirstCharLowercase(wsdtField.getWsdtFieldDesc().getRelatedEntity().getEntityName()) + "Service;\n";
                        }
                    }
                }
            }
        }
        return code;
    }
}
